var FU = {};
(function (exports) {
    var Urls = {
        treeDataUrl : "/pub/zy/data/tree",
        composePdfUrl : "/pub/zy/data/composePdf",
        generateRelatedUrl : "/pub/zy/data/generateRelated"
    }

    function operateFormatter(value,row,index) {
        console.log(row);
        var dataType = row["dataType"];
        var html = [];
        html.push('<a href="javascript:void(0);" onClick="FU.composePdf(\''+ row["id"] + '\',\''+ row["dataType"] + '\')" class="fa fa-file-pdf-o" title="合成PDF"></a>')
        if(dataType == "singleProject") {
            html.push('<a href="javascript:void(0);" onClick="FU.generateRelated(\''+ row["id"] + '\')" class="fa fa-link" title="关联PDF"></a>')
        }
        return html.join("&nbsp;")
    }

    function loadFilter(result) {
        $.each(result, function (index, data) {
            var dataType = data["dataType"];
            if(dataType == "file" || dataType == "record") {
                data["name"] = data["order_number"] + "-" + data["name"];
            }
        })
        return result;
    }

    function onBeforeExpand(row) {
        console.log(row);
        var dataType = row["dataType"];
        var paramData = {};
        if(dataType == "singleProject") {
            paramData = {
                dataType: dataType,
                singProjectId: row["id"]
            }
        } else if(dataType == "file") {
            paramData = {
                dataType: dataType,
                fileId: row["id"]
            }
        } else if(dataType == "record") {
            paramData = {
                dataType: dataType,
                recordId: row["id"]
            }
        }
        $("#maindatagrid").treegrid("options").queryParams = paramData;
        return true;
    }

    /**
     * 合成PDF
     */
    function composePdf(id, dataType) {
        EasyUiUtil.confirm("确认","确认合成PDF", function () {
            $.iMessager.progress({text: 'PDF合成中'});
            BaseUtil.doPost(Urls.composePdfUrl, {id: id, dataType: dataType}, function () {
                $.iMessager.progress("close");
            }, function () {
                $.iMessager.progress("close");
            })
        });
    }

    /**
     * 生成关联关系
     */
    function generateRelated(singleProjectId) {
        EasyUiUtil.confirm("确认","确认关联PDF", function () {
            $.iMessager.progress({text: 'PDF关联中'});
            BaseUtil.doPost(Urls.generateRelatedUrl, {singleProjectId: singleProjectId}, function () {
                $.iMessager.progress("close");
            }, function () {
                $.iMessager.progress("close");
            })
        });
    }

    exports.Urls = Urls;
    exports.operateFormatter = operateFormatter;
    exports.loadFilter = loadFilter;
    exports.onBeforeExpand = onBeforeExpand;
    exports.composePdf = composePdf;
    exports.generateRelated = generateRelated;
})(FU);